package ribbitmq_4;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.Scanner;
import java.util.UUID;
import java.util.concurrent.TimeoutException;
//陆游模式实现匹配陆陆游建模式
public class Consumer_4 {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory c = new ConnectionFactory();
        c.setHost("192.168.64.140");
        c.setPort(5672);
        c.setUsername("admin");
        c.setPassword("admin");

        Channel ch = c.newConnection().createChannel();
        //定义随机队列名
        String uuidname = UUID.randomUUID().toString();
        ch.queueDeclare(uuidname,
                false,
                true,
                true,
                null);
        //定义陆游模式的交换机
        ch.exchangeDeclare("logsDIRECT", BuiltinExchangeType.DIRECT);
        //输入绑定建空格隔开可多次绑定交换机
        System.out.println("输入绑定建空格隔开可多次绑定交换机");
        String string = new Scanner(System.in).nextLine();
        String[] str = string.split("\\s+");
        for (String s:str){
            ch.queueBind(uuidname, "logsDIRECT", s);
        }

        //正常消费
        DeliverCallback deliverCallback = new DeliverCallback() {
            @Override
            public void handle(String consumerTag, Delivery message) throws IOException {
                byte[] bytes = message.getBody();
                String msg = new String(bytes);
                //获取陆游建
                String str = message.getEnvelope().getRoutingKey();
                System.out.println("以收到消息" + msg+"-建"+str);
            }
        };

        CancelCallback cancelCallback = new CancelCallback() {
            @Override
            public void handle(String consumerTag) throws IOException {

            }
        };

        ch.basicConsume(uuidname, true, deliverCallback, cancelCallback);

    }

}
